package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.ocr.PDFUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.system.domain.SysFileLog;
import com.ruoyi.system.mapper.SysFileLogMapper;
import com.ruoyi.system.service.ISysFileLogService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;

@Service
public class SysFileLogServiceImpl extends ServiceImpl<SysFileLogMapper, SysFileLog> implements ISysFileLogService {
    @Override
    public AjaxResult uploadFile(MultipartFile file) {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
//            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
//            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            //保存文件上传日志
            SysFileLog sysFileLog = new SysFileLog();
            sysFileLog.setFileName(file.getOriginalFilename());
            sysFileLog.setFileSize(file.getSize());
            filePath = fileName.substring((Constants.RESOURCE_PREFIX).length());
            sysFileLog.setFilePath(filePath);
            String absolutePath = RuoYiConfig.getProfile()+filePath;
            sysFileLog.setFileAbsolutePath(absolutePath);
            sysFileLog.setFileType(file.getContentType());
            sysFileLog.setCreateTime(new Date());
            this.save(sysFileLog);
            ajax.put("filePath", filePath);
            ajax.put("fileLogId", sysFileLog.getId());
            return ajax;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public AjaxResult PDF2JPG(File file) {
        File f = PDFUtils.pdf2Jpg(file);
        SysFileLog sysFileLog = new SysFileLog();
        sysFileLog.setFileName(f.getName());
        sysFileLog.setFileSize(file.length());
        sysFileLog.setFilePath(file.getAbsolutePath().substring(RuoYiConfig.getUploadPath().length()));
        sysFileLog.setFileAbsolutePath(f.getAbsolutePath());
        sysFileLog.setCreateTime(new Date());
        this.save(sysFileLog);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("file", f);
        ajax.put("fileLogId", sysFileLog.getId());
        return ajax;
    }

    @Override
    public int uploadFileSuccess(Long templateFileId) {
        return baseMapper.uploadFileSuccess(templateFileId);
    }
}
